package com.spring.sperapc.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spring.sperapc.web.service.AppService;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.*;

/**
 * Created by wojunyu on 2015/7/25.
 */
@Controller
@RequestMapping(value = "/app")
@ApiIgnore
public class AppController {
    static Logger logger = Logger.getLogger(AppController.class.getName());

    @Resource
    private AppService appService;

    /**
     * @param age_group
     * @param rule_id
     * @param exam_type 可选
     * @return
     */
    @RequestMapping(value = "/getExam")
    @ResponseBody
    public Object getExam(String age_group, String rule_id,
                          String exam_type) {
        Map m = new HashMap();
//        List<Quest> questList = appService.selectQuestByType(Long.parseLong("1"));
//        m.put("quest_contexts", questList);
//        m.put("status", "1");
//        m.put("msg", "取数成功");
        return m;
    }

    @RequestMapping(value = "/getJson")
    @ResponseBody
    public Object getJson() {

        String jsonStr = "{\n" +
                "  \"exam_id\":\"1\",\n" +
                "  \"exam_title\":\"试卷1\",\n" +
                "  \"baby_id\":\"1\",\n" +
                "  \"user_id\":\"1\",\n" +
                "  \"rule_setting_id\":\"1\",\n" +
                "  \"dataset\":\n" +
                "  [{\n" +
                "    \"question_index\":\"1\",\n" +
                "    \"question_id\":\"1\",\n" +
                "    \"user_option\":\"A\"\n" +
                "  },{\n" +
                "    \"question_index\":\"2\",\n" +
                "    \"question_id\":\"2\",\n" +
                "    \"user_option\":\"A\"\n" +
                "  },{\n" +
                "    \"question_index\":\"3\",\n" +
                "    \"question_id\":\"3\",\n" +
                "    \"user_option\":\"A\"\n" +
                "  },{\n" +
                "    \"question_index\":\"4\",\n" +
                "    \"question_id\":\"4\",\n" +
                "    \"user_option\":\"A\"\n" +
                "  }]\n" +
                "}\n";
        
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>(){});
        Set<String> key = map.keySet();
        Iterator<String> iter = key.iterator();
        while (iter.hasNext()) {
            String field = iter.next();
            System.out.println(field + ":" + map.get(field));
            if ("dataset".equalsIgnoreCase(field)) {
                List<Map<String, Object>> list = JSON.parseObject(map.get(field) + "", new TypeReference<List<Map<String, Object>>>(){});
                for(Map<String, Object> maps:list){
                    Set<String> set = maps.keySet();
                    for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
                        String questskey = it.next();
                        System.out.println(questskey + ":" + maps.get(questskey));
                    }
                }

            }
        }
        return "OK";
    }

//    private ObjectMapper objectMapper = null;
//
//    @RequestMapping(value = "/getJson")
//    public void getJson(){
//        String jsonStr = "{\n" +
//                "  \"exam_id\":\"1\",\n" +
//                "  \"exam_title\":\"试卷1\",\n" +
//                "  \"baby_id\":\"1\",\n" +
//                "  \"user_id\":\"1\",\n" +
//                "  \"rule_setting_id\":\"1\",\n" +
//                "  \"dataset\":\n" +
//                "  [{\n" +
//                "    \"question_index\":\"1\",\n" +
//                "    \"question_id\":\"1\",\n" +
//                "    \"user_option\":\"A\"\n" +
//                "  },{\n" +
//                "    \"question_index\":\"2\",\n" +
//                "    \"question_id\":\"2\",\n" +
//                "    \"user_option\":\"A\"\n" +
//                "  },{\n" +
//                "    \"question_index\":\"3\",\n" +
//                "    \"question_id\":\"3\",\n" +
//                "    \"user_option\":\"A\"\n" +
//                "  },{\n" +
//                "    \"question_index\":\"4\",\n" +
//                "    \"question_id\":\"4\",\n" +
//                "    \"user_option\":\"A\"\n" +
//                "  }]\n" +
//                "}\n";
//        objectMapper = new ObjectMapper();
//        try {
//            Map<String, Map<String, Object>> maps = objectMapper.readValue(jsonStr, Map.class);
//            Set<String> key = maps.keySet();
//            Iterator<String> iter = key.iterator();
//            while (iter.hasNext()) {
//                String field = iter.next();
//                System.out.println(field + ":" + maps.get(field));
//                if("dataset".equalsIgnoreCase(field)){
//                    String jj = "[\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"1\",\n" +
//                            "    \"question_id\": \"1\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"2\",\n" +
//                            "    \"question_id\": \"2\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"3\",\n" +
//                            "    \"question_id\": \"3\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"4\",\n" +
//                            "    \"question_id\": \"4\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  }\n" +
//                            "]";
//                    List<HashMap<String, Object>> quests = objectMapper.readValue(jj, List.class);
//                    for (int i = 0; i < quests.size(); i++) {
//                        Map<String, Object> map = quests.get(i);
//                        Set<String> set = map.keySet();
//                        for (Iterator<String> it = set.iterator();it.hasNext();) {
//                            String questskey = it.next();
//                            System.out.println(questskey + ":" + map.get(questskey));
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    @RequestMapping(value = "/getJsonList")
//    public void getJsonList(){
//        objectMapper = new ObjectMapper();
//        try {
//                    String jj = "[\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"1\",\n" +
//                            "    \"question_id\": \"1\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"2\",\n" +
//                            "    \"question_id\": \"2\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"3\",\n" +
//                            "    \"question_id\": \"3\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  },\n" +
//                            "  {\n" +
//                            "    \"question_index\": \"4\",\n" +
//                            "    \"question_id\": \"4\",\n" +
//                            "    \"user_option\": \"A\"\n" +
//                            "  }\n" +
//                            "]";
//                    List<HashMap<String, Object>> quests = objectMapper.readValue(jj, List.class);
//                    for (int i = 0; i < quests.size(); i++) {
//                        Map<String, Object> map = quests.get(i);
//                        Set<String> set = map.keySet();
//                        for (Iterator<String> it = set.iterator();it.hasNext();) {
//                            String questskey = it.next();
//                            System.out.println(questskey + ":" + map.get(questskey));
//                        }
//                    }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
}
